package com.medical.standard.test;

import com.medicalstandard.entity.DocumentTemplate;
import com.medicalstandard.entity.Standard;
import com.medicalstandard.service.DataInputService;

import java.util.List;
import java.util.Scanner;

public class DocumentDemo {
    private static DataInputService service = new DataInputService();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("=== 医疗器械产品技术要求文档创建系统 ===");

        while (true) {
            showMenu();
            int choice = getIntInput("请选择操作: ");

            switch (choice) {
                case 1:
                    createNewDocument();
                    break;
                case 2:
                    updateDocument();
                    break;
                case 3:
                    deleteDocument();
                    break;
                case 4:
                    viewAllDocuments();
                    break;
                case 5:
                    viewDocumentDetails();
                    break;
                case 6:
                    testAutoNumbering();
                    break;
                case 0:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void showMenu() {
        System.out.println("\n========== 文档管理菜单 ==========");
        System.out.println("1. 创建新文档");
        System.out.println("2. 修改文档");
        System.out.println("3. 删除文档");
        System.out.println("4. 查看所有文档");
        System.out.println("5. 查看文档详情");
        System.out.println("6. 测试自动编号功能");
        System.out.println("0. 退出系统");
        System.out.println("================================");
    }

    private static void createNewDocument() {
        System.out.println("\n--- 创建新产品技术要求文档 ---");

        // 显示现有标准供选择
        List<Standard> standards = service.getAllStandards();
        if (standards.isEmpty()) {
            System.out.println("请先添加标准信息！");
            return;
        }

        System.out.println("现有标准列表:");
        for (Standard standard : standards) {
            System.out.println("ID: " + standard.getId() + " - " + standard.getStandardCode() + " - " + standard.getStandardName());
        }

        DocumentTemplate document = new DocumentTemplate();

        System.out.print("产品名称: ");
        document.setProductName(scanner.nextLine());

        System.out.print("产品型号: ");
        document.setProductModel(scanner.nextLine());

        int standardId = getIntInput("选择标准ID: ");
        document.setStandardId(standardId);

        System.out.print("性能指标: ");
        document.setPerformanceIndicators(scanner.nextLine());

        System.out.print("检验方法: ");
        document.setInspectionMethods(scanner.nextLine());

        System.out.print("产品型号/规格说明: ");
        document.setProductSpecification(scanner.nextLine());

        System.out.print("术语: ");
        document.setTerminology(scanner.nextLine());

        System.out.print("创建者: ");
        document.setCreatedBy(scanner.nextLine());

        boolean result = service.createDocument(document);
        if (result) {
            System.out.println("✓ 文档创建成功！");
            System.out.println("✓ 自动生成的文档编号: " + document.getDocumentNumber());
        } else {
            System.out.println("✗ 文档创建失败！");
        }
    }

    private static void updateDocument() {
        System.out.println("\n--- 修改文档 ---");
        viewAllDocuments();

        int documentId = getIntInput("请输入要修改的文档ID: ");
        DocumentTemplate document = service.getDocumentById(documentId);

        if (document == null) {
            System.out.println("✗ 未找到该文档！");
            return;
        }

        System.out.println("当前文档信息:");
        System.out.println("文档编号: " + document.getDocumentNumber());
        System.out.println("产品名称: " + document.getProductName());
        System.out.println("产品型号: " + document.getProductModel());

        System.out.print("新产品名称 (当前: " + document.getProductName() + "): ");
        String productName = scanner.nextLine();
        if (!productName.isEmpty()) document.setProductName(productName);

        System.out.print("新产品型号 (当前: " + document.getProductModel() + "): ");
        String productModel = scanner.nextLine();
        if (!productModel.isEmpty()) document.setProductModel(productModel);

        System.out.print("新性能指标 (输入空值保持不变): ");
        String performance = scanner.nextLine();
        if (!performance.isEmpty()) document.setPerformanceIndicators(performance);

        System.out.print("新状态 (草稿/已完成): ");
        String status = scanner.nextLine();
        if (!status.isEmpty()) document.setStatus(status);

        boolean result = service.updateDocument(document);
        if (result) {
            System.out.println("✓ 文档修改成功！");
        } else {
            System.out.println("✗ 文档修改失败！");
        }
    }

    private static void deleteDocument() {
        System.out.println("\n--- 删除文档 ---");
        viewAllDocuments();

        int documentId = getIntInput("请输入要删除的文档ID: ");

        System.out.print("确认删除？(y/n): ");
        String confirm = scanner.nextLine();
        if (!confirm.equalsIgnoreCase("y")) {
            System.out.println("取消删除操作");
            return;
        }

        boolean result = service.deleteDocument(documentId);
        if (result) {
            System.out.println("✓ 文档删除成功！");
        } else {
            System.out.println("✗ 文档删除失败！");
        }
    }

    private static void viewAllDocuments() {
        System.out.println("\n--- 所有文档列表 ---");
        List<DocumentTemplate> documents = service.getAllDocuments();

        if (documents.isEmpty()) {
            System.out.println("暂无文档！");
            return;
        }

        for (DocumentTemplate document : documents) {
            System.out.println("ID: " + document.getDocumentId() +
                    " | 编号: " + document.getDocumentNumber() +
                    " | 产品: " + document.getProductName() +
                    " | 型号: " + document.getProductModel() +
                    " | 状态: " + document.getStatus() +
                    " | 创建者: " + document.getCreatedBy() +
                    " | 日期: " + document.getCreateDate());
        }
        System.out.println("总计: " + documents.size() + " 个文档");
    }

    private static void viewDocumentDetails() {
        System.out.println("\n--- 查看文档详情 ---");
        viewAllDocuments();

        int documentId = getIntInput("请输入要查看的文档ID: ");
        DocumentTemplate document = service.getDocumentById(documentId);

        if (document == null) {
            System.out.println("✗ 未找到该文档！");
            return;
        }

        System.out.println("\n=== 文档详情 ===");
        System.out.println("文档编号: " + document.getDocumentNumber());
        System.out.println("产品名称: " + document.getProductName());
        System.out.println("产品型号: " + document.getProductModel());
        System.out.println("标准ID: " + document.getStandardId());
        System.out.println("性能指标: " + document.getPerformanceIndicators());
        System.out.println("检验方法: " + document.getInspectionMethods());
        System.out.println("产品规格说明: " + document.getProductSpecification());
        System.out.println("术语: " + document.getTerminology());
        System.out.println("创建者: " + document.getCreatedBy());
        System.out.println("创建日期: " + document.getCreateDate());
        System.out.println("状态: " + document.getStatus());
    }

    private static void testAutoNumbering() {
        System.out.println("\n=== 测试自动编号功能 ===");

        // 连续创建几个测试文档来展示自动编号
        for (int i = 1; i <= 3; i++) {
            DocumentTemplate testDoc = new DocumentTemplate();
            testDoc.setProductName("测试产品" + i);
            testDoc.setProductModel("TEST-MODEL-" + i);
            testDoc.setStandardId(1); // 使用第一个标准
            testDoc.setPerformanceIndicators("测试性能指标" + i);
            testDoc.setInspectionMethods("测试检验方法" + i);
            testDoc.setProductSpecification("测试规格说明" + i);
            testDoc.setTerminology("测试术语" + i);
            testDoc.setCreatedBy("test_user");

            boolean result = service.createDocument(testDoc);
            if (result) {
                System.out.println("✓ 测试文档 " + i + " 创建成功");
                System.out.println("  文档编号: " + testDoc.getDocumentNumber());
            } else {
                System.out.println("✗ 测试文档 " + i + " 创建失败");
            }
        }

        System.out.println("自动编号功能测试完成！");
    }

    private static int getIntInput(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }
    }
}